package com.aube.plugin.rank.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.huyn.baseframework.dynamicload.DLView;

/**
 * Created by huyaonan on 16/3/28.
 */
public class RankNum extends DLView {

    private Paint mPaint;
    private Paint mTextPaint;
    private int mNum=0;

    public RankNum(Context context) {
        this(context, null);
    }
    public RankNum(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public RankNum(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(14);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(mNum <= 0)
            return;

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        //画圆角矩形
        mPaint.setStyle(Paint.Style.FILL);
        RectF oval = new RectF(0, 0, width, height);
        canvas.drawRoundRect(oval, height/2, height/2, mPaint);

        float tw = mTextPaint.measureText(mNum+"");
        float dy = height/2 + mTextPaint.getFontMetrics().bottom;
        canvas.drawText(""+mNum, (width-tw)/2, dy, mTextPaint);
    }

    public void setTextAndSize(int num, int size) {
        this.mNum = num;
        mTextPaint.setTextSize(size);
        invalidate();
    }

    public void setText(int num) {
        this.mNum = num;
        invalidate();
    }

    public void updateColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    public void updateNo(int num) {
        this.mNum = num;
        invalidate();
    }

}
